package p.ithorns.sample.office.pdf;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontProvider;
import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.options.PDFOptions;
import io.woo.htmltopdf.HtmlToPdf;
import io.woo.htmltopdf.HtmlToPdfObject;
import org.htmlunit.BrowserVersion;
import org.htmlunit.NicelyResynchronizingAjaxController;
import org.htmlunit.WebClient;
import org.htmlunit.WebClientOptions;
import org.htmlunit.html.DomChangeEvent;
import org.htmlunit.html.DomChangeListener;
import org.htmlunit.html.HtmlElement;
import org.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.FileOutputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Set;

/**
 * TestMain
 *
 * @author HtL
 * @date 2025/3/3 10:04
 * @since 1.0.0
 */
public class TestMain {

    public static void main(String[] args) throws Exception {
        String url = "http://10.20.32.60:7200/spw-app/special-work/ticket-apply/ticket-record/185?tab=2";
        try {
            // testSelenium(url);
            // testHtmltopdf("https://github.com/wooio/htmltopdf-java");
            // testJvppeteer(url);
            // testIText("http://10.20.32.60:7200", "/spw-app/special-work/ticket-perform/view/186/record");
            // testJsoup(url);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }


    static void testHtmltopdf(String url) {
        HtmlToPdfObject htmlToPdfObject = HtmlToPdfObject.forUrl(url);
        HtmlToPdf.create()
                .object(htmlToPdfObject)
                .convert("output.pdf");
    }

    static void testJvppeteer(String url) throws Exception {
        ArrayList<String> argList = new ArrayList<>();
        //自动下载，第一次下载后不会再下载
        // BrowserFetcher.downloadIfNotExist(null);
        LaunchOptions options = new LaunchOptionsBuilder().withArgs(argList).withHeadless(false).build();
        argList.add("--no-sandbox");
        argList.add("--disable-setuid-sandbox");
        Puppeteer.launch(options);

        Browser browser = Puppeteer.launch(options);
        Page page = browser.newPage();
        page.goTo(url);
        PDFOptions pdfOptions = new PDFOptions();
        pdfOptions.setPath("test.pdf");
        page.pdf(pdfOptions);
        page.close();
        browser.close();
    }

    static void testIText(String baseUrl, String html) {
//        try {
//            // HTML文件的网址
//            // URL url = new URL(baseUrl + path);
//            // 读取HTML内容
//            // String htmlContent = new String(url.openStream().readAllBytes(), StandardCharsets.UTF_8);
//            // PDF的输出路径
//            String pdfFilePath = "output.pdf";
//
//            // 创建PDF文档
//            PdfWriter writer = new PdfWriter(pdfFilePath);
//            PdfDocument pdf = new PdfDocument(writer);
//            pdf.setDefaultPageSize(PageSize.A4);
//
//            // 设置字体
//            FontProvider fontProvider = new FontProvider();
//            // fontProvider.addFont(PdfFontFactory.createFont().getFontProgram(), "UniGB-UCS2-H");
//
//            PdfFont sysFont = PdfFontFactory.createFont("C:/Windows/Fonts/simsun.ttc,0", PdfEncodings.IDENTITY_H);
//            fontProvider.addFont(sysFont.getFontProgram());
//
//            ConverterProperties properties = new ConverterProperties()
//                    .setBaseUri(baseUrl)
//                    .setFontProvider(fontProvider);
//
//            // 将HTML转换为PDF
//            HtmlConverter.convertToPdf(html, pdf, properties);
//            System.out.println("PDF生成成功！");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    static void testJsoup(String url) throws IOException {
        // Document doc = Jsoup.parse(new File("test.html"));
        Document doc = Jsoup.connect(url).get();
        String html = doc.html();
        String outputFilePath = "output.pdf";

        // 将HTML转换为PDF
        try (FileOutputStream fos = new FileOutputStream(outputFilePath)) {
            HtmlConverter.convertToPdf(html, fos);
        }
    }

}